package com.irdstudio.efp.limit.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.irdstudio.basic.framework.core.base.AbstractFrameworkService;
import com.irdstudio.basic.framework.core.base.DataOptionalAuthorityDao;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.efp.limit.service.dao.LmtPrelistBatchAppDao;
import com.irdstudio.efp.limit.service.domain.LmtPrelistBatchApp;
import com.irdstudio.efp.limit.service.facade.LmtPrelistBatchAppService;
import com.irdstudio.efp.limit.service.vo.LmtPrelistBatchAppVO;
/**
 * <p>ServiceImpl: 预授信名单批次申请表				<p>
 * @author chenqm1
 * @date 2018-05-04
 */
@Service("lmtPrelistBatchAppService")
public class LmtPrelistBatchAppServiceImpl extends AbstractFrameworkService implements LmtPrelistBatchAppService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(LmtPrelistBatchAppServiceImpl.class);

	@Autowired
	private LmtPrelistBatchAppDao lmtPrelistBatchAppDao;
	
	/**
	 * 新增操作
	 */
	public int insertLmtPrelistBatchApp(LmtPrelistBatchAppVO inLmtPrelistBatchAppVo) {
		logger.debug("当前新增数据为:"+ inLmtPrelistBatchAppVo.toString());
		int num = 0;
		try {
			LmtPrelistBatchApp lmtPrelistBatchApp = new LmtPrelistBatchApp();
			beanCopy(inLmtPrelistBatchAppVo, lmtPrelistBatchApp);
			num = lmtPrelistBatchAppDao.insertLmtPrelistBatchApp(lmtPrelistBatchApp);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(LmtPrelistBatchAppVO inLmtPrelistBatchAppVo) {
		logger.debug("当前删除数据条件为:"+ inLmtPrelistBatchAppVo);
		int num = 0;
		try {
			LmtPrelistBatchApp lmtPrelistBatchApp = new LmtPrelistBatchApp();
			beanCopy(inLmtPrelistBatchAppVo, lmtPrelistBatchApp);
			num = lmtPrelistBatchAppDao.deleteByPk(lmtPrelistBatchApp);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inLmtPrelistBatchAppVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(LmtPrelistBatchAppVO inLmtPrelistBatchAppVo) {
		logger.debug("当前修改数据为:"+ inLmtPrelistBatchAppVo.toString());
		int num = 0;
		try {
			LmtPrelistBatchApp lmtPrelistBatchApp = new LmtPrelistBatchApp();
			beanCopy(inLmtPrelistBatchAppVo, lmtPrelistBatchApp);
			num = lmtPrelistBatchAppDao.updateByPk(lmtPrelistBatchApp);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inLmtPrelistBatchAppVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public LmtPrelistBatchAppVO queryByPk(LmtPrelistBatchAppVO inLmtPrelistBatchAppVo) {
		
		logger.debug("当前查询参数信息为:"+ inLmtPrelistBatchAppVo);
		try {
			LmtPrelistBatchApp queryLmtPrelistBatchApp = new LmtPrelistBatchApp();
			beanCopy(inLmtPrelistBatchAppVo, queryLmtPrelistBatchApp);
			LmtPrelistBatchApp queryRslLmtPrelistBatchApp = lmtPrelistBatchAppDao.queryByPk(queryLmtPrelistBatchApp);
			if (Objects.nonNull(queryRslLmtPrelistBatchApp)) {
				LmtPrelistBatchAppVO outLmtPrelistBatchAppVo = beanCopy(queryRslLmtPrelistBatchApp, new LmtPrelistBatchAppVO());
				logger.debug("当前查询结果为:"+ outLmtPrelistBatchAppVo.toString());
				return outLmtPrelistBatchAppVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<LmtPrelistBatchAppVO> queryAllOwner(LmtPrelistBatchAppVO lmtPrelistBatchAppVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:" + JSON.toJSONString(lmtPrelistBatchAppVo));
		LmtPrelistBatchApp lmtPrelistBatchApp = beanCopy(lmtPrelistBatchAppVo, new LmtPrelistBatchApp());
		List<LmtPrelistBatchAppVO> list = null;
		try {
			List<LmtPrelistBatchApp> lmtPrelistBatchApps = lmtPrelistBatchAppDao.queryAllOwnerByPage(lmtPrelistBatchAppVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ lmtPrelistBatchApps.size());
			pageSet(lmtPrelistBatchApps, lmtPrelistBatchAppVo);
			list = (List<LmtPrelistBatchAppVO>) beansCopy(lmtPrelistBatchApps, LmtPrelistBatchAppVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<LmtPrelistBatchAppVO> queryAllCurrOrg(LmtPrelistBatchAppVO lmtPrelistBatchAppVo) {
		logger.debug("当前查询本人所属机构数据信息的参数信息为:" + JSON.toJSONString(lmtPrelistBatchAppVo));
		List<LmtPrelistBatchAppVO> list = null;
		try {
			LmtPrelistBatchApp lmtPrelistBatchApp = beanCopy(lmtPrelistBatchAppVo, new LmtPrelistBatchApp());
			List<LmtPrelistBatchApp> lmtPrelistBatchApps = lmtPrelistBatchAppDao.queryAllCurrOrgByPage(lmtPrelistBatchAppVo);
			logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+lmtPrelistBatchApps.size());
			pageSet(lmtPrelistBatchApps, lmtPrelistBatchAppVo);
			list = (List<LmtPrelistBatchAppVO>) beansCopy(lmtPrelistBatchApps, LmtPrelistBatchAppVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<LmtPrelistBatchAppVO> queryAllCurrDownOrg(LmtPrelistBatchAppVO lmtPrelistBatchAppVo) {
		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:"  + JSON.toJSONString(lmtPrelistBatchAppVo));
		List<LmtPrelistBatchAppVO> list = null;
		try {
			LmtPrelistBatchApp lmtPrelistBatchApp = beanCopy(lmtPrelistBatchAppVo, new LmtPrelistBatchApp());
			List<LmtPrelistBatchApp> lmtPrelistBatchApps = lmtPrelistBatchAppDao.queryAllCurrDownOrgByPage(lmtPrelistBatchAppVo);
			logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ lmtPrelistBatchApps.size());
			pageSet(lmtPrelistBatchApps, lmtPrelistBatchAppVo);
			list = (List<LmtPrelistBatchAppVO>) beansCopy(lmtPrelistBatchApps, LmtPrelistBatchAppVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int deleteByBatchSernoList(List<LmtPrelistBatchAppVO> lmtPrelistBatchAppVos) {
		List<LmtPrelistBatchApp> llpba = new ArrayList<LmtPrelistBatchApp>();
		int num = 0;
		try {
			if(lmtPrelistBatchAppVos != null && lmtPrelistBatchAppVos.size() > 0){
				llpba = (List<LmtPrelistBatchApp>) beansCopy(lmtPrelistBatchAppVos, LmtPrelistBatchApp.class);
				for (LmtPrelistBatchApp lmtPrelistBatchApp : llpba) {
					num = lmtPrelistBatchAppDao.deleteByPk(lmtPrelistBatchApp);
				}
				return num;
			}else{
				return num;
			}
		} catch (Exception e) {
			logger.error("删除数据出现异常!", e);
			return -1;
		}
	}

	@Override
	public DataOptionalAuthorityDao getDataAuthorityDao() {
		return this.lmtPrelistBatchAppDao;
	}
}
